package com.hzncc.scaffold.controller.Front.TrainingProgram;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.config.MinioConfig;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Major;
import com.hzncc.scaffold.entity.Other.Trainingprogram;
import com.hzncc.scaffold.entity.Other.Trainingprogramcourse;
import com.hzncc.scaffold.entity.Vo.FirstAndSecondVo;
import com.hzncc.scaffold.entity.Vo.ProgramDetailVo;
import com.hzncc.scaffold.entity.Vo.TrainingProgramCourseVo;
import com.hzncc.scaffold.entity.Vo.TrainingprogramVo;
import com.hzncc.scaffold.mapper.TrainingprogramMapper;
import com.hzncc.scaffold.service.CourseService;
import com.hzncc.scaffold.service.TrainingprogramService;
import com.hzncc.scaffold.service.TrainingprogramcourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-07 15:57
 * @Description: null
 */
@Api(tags = "前台接口-人才培养方案")
@RequestMapping("/trainingProgram")
@RestController
public class trainingProgramController {

    @Autowired
    TrainingprogramMapper trainingprogramMapper;

    @Autowired
    TrainingprogramService trainingprogramService;

    @Autowired
    TrainingprogramcourseService trainingprogramcourseService;

    @Autowired
    CourseService courseService;

    @Autowired
    MinioConfig minioConfig;

    @Value("${project.imgFileSuffix}")
    public String imgFileSuffix;

    @GetMapping("/list")
    @ApiOperation("人才培养方案列表")
    public ResultT list(){

        FirstAndSecondVo firstAndSecond = new FirstAndSecondVo();

        ArrayList<TrainingprogramVo> first = new ArrayList<>();
        ArrayList<TrainingprogramVo> second = new ArrayList<>();

        List<TrainingprogramVo> trainingprogramVos = trainingprogramMapper.selectTrainingList();

        for (TrainingprogramVo trainingprogramVo : trainingprogramVos) {
            Major major = new Major();
            major.setId(trainingprogramVo.getMajorId());
            major.setName(trainingprogramVo.getMajorName());
            trainingprogramVo.setCover(imgFileSuffix+trainingprogramVo.getCover() );
            trainingprogramVo.setMajor(major);

            if(trainingprogramVo.getEdPhase()==0){
                first.add(trainingprogramVo);
            }
            if(trainingprogramVo.getEdPhase()==1){
                second.add(trainingprogramVo);
            }
        }

        firstAndSecond.setFirst(first);
        firstAndSecond.setSecond(second);

        return ResultT.success(firstAndSecond);
    }

    @GetMapping("/detail")
    @ApiOperation("获取人才培养方案详情")
    public ResultT detail(Integer programId){
        if(trainingprogramService.getById(programId)==null){
            return ResultT.fail("数据错误");
        }
        Trainingprogram trainingprogram = trainingprogramService.getById(programId);

        ProgramDetailVo programDetailVo = BeanUtil.toBean(trainingprogram, ProgramDetailVo.class);

        ArrayList<TrainingProgramCourseVo> ProgramCourse = new ArrayList<>();

        LambdaQueryWrapper<Trainingprogramcourse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(programDetailVo.getId()!=null,Trainingprogramcourse::getTrainingProgramId,programDetailVo.getId());

        List<Trainingprogramcourse> trainingprogramcourseList = trainingprogramcourseService.list(lqw);

        for (Trainingprogramcourse trainingprogramcourse : trainingprogramcourseList) {
            if(trainingprogramcourse.getCourseId()!=null){

                TrainingProgramCourseVo trainingProgramCourseVo = BeanUtil.toBean(courseService.getById(trainingprogramcourse.getCourseId()), TrainingProgramCourseVo.class);
                trainingProgramCourseVo.setCover(imgFileSuffix+trainingProgramCourseVo.getCover());
                ProgramCourse.add(trainingProgramCourseVo);
            }
        }
        programDetailVo.setProgramCourse(ProgramCourse);

        return ResultT.success(programDetailVo);
    }
}
